package com.jiukuaidao.zhulin.module.order;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.base.BaseHolder;
import com.jiukuaidao.library.base.BaseXrecyclerViewAdapter;
import com.jiukuaidao.library.common.dialog.CommDialog;
import com.jiukuaidao.library.common.dialog.ICallBack;
import com.jiukuaidao.library.util.ClearEditText;
import com.jiukuaidao.library.util.ImageGlideUtils;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.library.widget.recyclerview.XRecyclerView;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.entity.ApiOrderCommentPost;
import com.jiukuaidao.zhulin.entity.ApiOrderDetailInfo;
import com.jiukuaidao.zhulin.entity.EventRefreshComments;
import com.jiukuaidao.zhulin.module.order.contract.OrderCommentContract;
import com.jiukuaidao.zhulin.module.order.presenter.OrderCommentPresenter;
import com.jiukuaidao.zhulin.widget.MyRatingBar;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * T_winds
 * Date:  16.8.19
 * Description:
 */
public class OrdersCommentsActivity extends BaseActivity<OrderCommentContract.Presenter> implements OrderCommentContract.View {

    @BindView(R.id.titile_left_imageview)
    ImageView tv_back;
    @BindView(R.id.titile_text)
    TextView titleText;
    @BindView(R.id.common_bottom_text_value)
    TextView bottomText;
    @BindView(R.id.order_comment_footer)
    View orderFooter;
    @BindView(R.id.order_comment_time_value)
    TextView timeValue;
    @BindView(R.id.order_comment_value_value)
    TextView moneyValue;
    @BindView(R.id.order_comment_goods)
    XRecyclerView goodsRecyclerView;
    @BindView(R.id.comment_post_comments_rbar)
    MyRatingBar mPostRatBar;

    private String mOrderNo;


    private ApiOrderDetailInfo mOrderDetailInfo;
    private BaseXrecyclerViewAdapter goodsViewAdapter;

    private ApiOrderCommentPost mCommentPost;//提交评价的数据
    private List<ApiOrderCommentPost.GoodsCommnet> mGoodsCommnets;

    private static final String TAG = "OrdersCommentsActivity";

    /*************************************************
     * Native
     **************************************************/

    private void initXRecyclerView(final List<ApiOrderDetailInfo.GoodsListBean> list){

        goodsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        goodsViewAdapter = new BaseXrecyclerViewAdapter<ApiOrderDetailInfo.GoodsListBean>(this,R.layout.item_order_comment_goods,list,goodsRecyclerView) {
            @Override
            public void updateUI(BaseHolder holder, int position, final ApiOrderDetailInfo.GoodsListBean data) {

                ImageView goodsImg = holder.getView(R.id.item_order_comment_img);
                MyRatingBar goodsRatingBar = holder.getView(R.id.item_order_comment_rat_bar);
                ClearEditText commentEditText = holder.getView(R.id.item_order_comment_et);
                ImageGlideUtils.GlideImages(OrdersCommentsActivity.this, data.goodsImage, goodsImg, R.mipmap.ic_default_goods_list);

                goodsRatingBar.setOnRatingListener(new MyRatingBar.OnRatingListener() {
                    @Override
                    public void onRating(Object bindObject, int RatingSource) {
                        for (ApiOrderCommentPost.GoodsCommnet item :
                                mGoodsCommnets) {
                            if (item.goodsId.equals(data.goodsId)){
                                item.goodsGrade = RatingSource;
                            }
                        }
                    }
                });

                commentEditText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        for (ApiOrderCommentPost.GoodsCommnet item :
                                mGoodsCommnets) {
                            if (item.goodsId.equals(data.goodsId)){
                                item.goodsComment = s.toString();
                            }
                        }
                    }
                });
            }

        };

        goodsRecyclerView.setAdapter(goodsViewAdapter);


    }

    private void initView() {

        titleText.setText(R.string.order_comment_title);
        bottomText.setText(R.string.order_comment_bottom_text);
        goodsRecyclerView.setPullRefreshEnabled(false);
        goodsRecyclerView.setLoadingMoreEnabled(false);
        mCommentPost = new ApiOrderCommentPost();

        mPostRatBar.setOnRatingListener(new MyRatingBar.OnRatingListener() {
            @Override
            public void onRating(Object bindObject, int RatingSource) {
                if (mCommentPost == null){
                    mCommentPost = new ApiOrderCommentPost();
                }
                mCommentPost.orderGrade = RatingSource;
            }
        });

    }

    /**
     *
     * @param data
     */
    private void RefreshUI(ApiOrderDetailInfo data) {

        timeValue.setText(data.createTime);
        moneyValue.setText(String.format(getString(R.string.moneySymbol), data.totalAmount));

        if (null != goodsViewAdapter){
            goodsViewAdapter.setData(data.goodsList);
            goodsViewAdapter.notifyDataSetChanged();
        }else {
            initXRecyclerView(data.goodsList);
        }

    }

    private void showBackDialog(){
        showDialogTwoBtn("是否放弃评论订单？", "否", "是", new ICallBack.TwoCallBack() {
            @Override
            public void left(CommDialog dialog) {
            }

            @Override
            public void right(CommDialog dialog) {
                OrdersCommentsActivity.this.finish();
            }
        });
    }


    /**
     * 是否可以提交
     * @return
     */
    private boolean enablePost(){

        if (mCommentPost.orderGrade==null || mCommentPost.orderGrade <= 0){
            ToastUtil.show(R.string.order_comment_order_toast, Toast.LENGTH_LONG);
            return false;
        }

        for (ApiOrderCommentPost.GoodsCommnet item :
                mGoodsCommnets) {
            if (item.goodsGrade == null || item.goodsGrade <= 0){
                ToastUtil.show(R.string.order_comment_good_toast,Toast.LENGTH_LONG);
                return false;
            }
        }

        return true;
    }

    /***********************************************
     * View
     *************************************************/
    @Override
    public void setOrderNo(String orderNo) {
        this.mOrderNo = orderNo;
    }

    @Override
    public String getOrderNo() {
        return this.mOrderNo;
    }

    @Override
    public void getOrderDetailDataSuc(ApiOrderDetailInfo data) {
        mOrderDetailInfo = data;
        if (mCommentPost == null)
            mCommentPost = new ApiOrderCommentPost();
        if (mGoodsCommnets == null)
            mGoodsCommnets = new ArrayList<>();
        mCommentPost.orderNo = data.orderNo;
        for (ApiOrderDetailInfo.GoodsListBean item :
                data.goodsList) {
            ApiOrderCommentPost commentPost = new ApiOrderCommentPost();
            ApiOrderCommentPost.GoodsCommnet goodsCommnet = commentPost.new GoodsCommnet();
            goodsCommnet.goodsId = item.goodsId;
            goodsCommnet.goodsComment = "好评";
            mGoodsCommnets.add(goodsCommnet);
        }
        mCommentPost.goodsList = mGoodsCommnets;
        RefreshUI(data);

    }

    @Override
    public ApiOrderDetailInfo getOrderInfo() {
        return mOrderDetailInfo;
    }

    @Override
    public void postCommentSuc() {

        EventBus.getDefault().post(new EventRefreshComments());

        ToastUtil.show("提交成功",Toast.LENGTH_SHORT);
        this.finish();

    }

    @Override
    public ApiOrderCommentPost getCommentPost() {
        mCommentPost.goodsList = mGoodsCommnets;
        return mCommentPost;
    }

    /***********************************************
     * BaseActivity
     *************************************************/

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_order_comment;
    }

    @Override
    protected void handleBundle(Bundle bundle) {

        setOrderNo(bundle.getString("orderNumber", ""));

    }

    @Override
    protected void init() {

        initView();
        new OrderCommentPresenter(TaskProvider.provideTasksRepository(this), this);
        mPresenter.start();

    }

    @Override
    public void onBackPressed() {
        showBackDialog();
    }

    /**************************************
     * ButterKnife
     ***************************************************/

    @OnClick({R.id.order_comment_footer, R.id.titile_left_imageview})
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.order_comment_footer:
                if (enablePost()){
                    mPresenter.postComment();
                }
                break;
            case R.id.titile_left_imageview:
                showBackDialog();
                break;
        }

    }


}
